package no.hist.rtc.rtchatu;


import java.util.ArrayList;

import no.hist.rtc.rtchat.R;
import android.app.Activity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivityU extends Activity {
	private final int portnr = 12345;
	private final String TAG = "CHAT_U";
	
	private ArrayList<String> meldinger = new ArrayList<String>();

	public void postMld(String mld){
		meldinger.add(mld);
		updateChatScreen();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_u);
        meldinger.add(getMyIp());
        updateChatScreen();
        Log.d("TAGG", "onCreate i MainActivityU kalt...");
        new Task(this).start();
    }
    
	@SuppressWarnings("deprecation")
	private String getMyIp(){
    	WifiManager wm = (WifiManager)getSystemService(WIFI_SERVICE);
    	WifiInfo wii = wm.getConnectionInfo();
    	return Formatter.formatIpAddress(wii.getIpAddress());
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0,0,0,getString(R.string.avslutt));
        return true;
    }

    public void updateChatScreen(){
    	TextView tv = (TextView)findViewById(R.id.chatVindu);
    	tv.setText(makeText(meldinger));
    }

	private CharSequence makeText(ArrayList<String> meldinger2) {
		String txt = "";
		for(String s : meldinger)
			txt += s + "\n";
		return txt;
	}
    
	public boolean onOptionsItemSelected(MenuItem item){

    	switch(item.getItemId()){
	    	case 0:
	    		finish();
	    		return true;
    	}
		return false;
    }
}
